/*
 * DISPLAY.c
 *
 * Created: 24.09.2012 14:51:56
 *  Author: mariuvi
 */ 
#include "DISPLAY.h"
#include "SRAM.h"
#include "OLED.h"
#include "USART.h"
#include "definitions.h"

volatile char *DISPLAY_display = (char*) SRAM_ADR_DISPLAY;

char* DISPLAY_access(int row, int col)
{
	return (&(DISPLAY_display[25*row + col]));
}

void DISPLAY_init()
{
	DISPLAY_clear();
	DISPLAY_print();
	
}

void DISPLAY_print()
{
	int i,j;
	for(i = 0; i<8; i++)
	{
		for(j=0; j<25; j++)
		{
			OLED_write_char(DISPLAY_display[25*i + j]);	
		}
		OLED_fill_rest();
	}
	OLED_reset_pointer();
}

void DISPLAY_clear()
{
	int i,j;
	for(i = 0; i<8; i++)
	{
		for(j=0; j<25; j++)
		{
			DISPLAY_display[i*25 + j] = ' ';
		}
	}
}



void DISPLAY_move_marker(Direction dir)
{
	volatile char* display = DISPLAY_access(4, 0);
	if(dir == UP)
	{
		*(display) = ' ';
		*(display - 25) = '#';
	}
	else if(dir == DOWN)
	{
		*(display) = ' ';
		*(display + 25) = '#';	
	}
	else
	{
		*(display - 25) = ' ';
		display[0] = '#';
		*(display + 25) = ' ';
	}
}

void DISPLAY_write_line(const char* str, int row, int col)
{
	volatile char* display = DISPLAY_access(row, col);
	int i = 0;
	
	//Adds the string
	while(str[i] != '\0')
	{
		display[i] = str[i];
		i++;
	}
	
	//Clears the rest of the row
	for( i; i<25-col; i++)
	{
		display[i] = ' '; 
	}
}

void DISPLAY_move_menu(const char* str_above,const char* str_selected,const char* str_below)
{	
	DISPLAY_write_line(str_above, 3, 3);
	DISPLAY_write_line(str_selected, 4, 3);
	DISPLAY_write_line(str_below, 5, 3);
}	